
package com.wyzz.hr.action;

import java.util.*;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;

import com.wyzz.hr.base.BaseDownloadAction;
import com.wyzz.hr.base.ComonBeans;
import com.wyzz.hr.bo.IClientBO;
import com.wyzz.hr.bo.IEmpQuitBo;
import com.wyzz.hr.bo.IPositionBo;
import com.wyzz.hr.bo.UserBo;
import com.wyzz.hr.pojo.Department;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Empquit;
import com.wyzz.hr.pojo.Position;
import com.wyzz.hr.pojo.Userinfo;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.BaseCrit;
import com.wyzz.hr.util.Pager;

public class EmpQuit extends BaseDownloadAction {

	public EmpQuit() {
		page = new Pager();
		outputIoName = "OEmpQuit";
		searchOrExport = null;
	}

	public String list() throws Exception {
		getDrillDownList();
		quitList = searchQuitEmp();
		if ("export".equals(searchOrExport))
			return downloadToExcel(quitList, outputIoName, outmatchModelId,
					"employee");
		else
			return "success";
	}

	private List searchQuitEmp() {
		DetachedCriteria dcQuitEmp = searchQuitWithEmp_DC();
		addCriteria(dcQuitEmp);
		IEmpQuitBo empQuit = (IEmpQuitBo) getBean("empQuitBo");
		return empQuit.findQuitEmp(dcQuitEmp, page, searchOrExport);
	}

	private DetachedCriteria searchQuitWithEmp_DC() {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Empquit.class);
		dc.createAlias(Empquit.PROP_EMPLOYEE, "emp", 1);
		dc.createAlias((new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_DEPT_NO).toString(), "emp.empDeptNo", 1);
		dc.createAlias(Empquit.PROP_EQ_DEPT_NO, "department", 1);
		dc.createAlias(Empquit.PROP_EQ_PERMISSION, "manager", 1);
		if ("export".equals(searchOrExport)) {
			dc.createAlias((new StringBuilder()).append("emp.").append(
					Employee.PROP_EMP_PB_NO).toString(),
					Employee.PROP_EMP_PB_NO, 1);
			dc.createAlias((new StringBuilder()).append("emp.").append(
					Employee.PROP_EMP_LOCATION_NO).toString(),
					Employee.PROP_EMP_LOCATION_NO, 1);
			dc.createAlias((new StringBuilder()).append("emp.").append(
					Employee.PROP_EMP_TYPE).toString(), Employee.PROP_EMP_TYPE,
					1);
			dc.createAlias((new StringBuilder()).append("emp.").append(
					Employee.PROP_EMP_BENEFIT_TYPE).toString(),
					Employee.PROP_EMP_BENEFIT_TYPE, 1);
		}
		return dc;
	}

	public void addCriteria(DetachedCriteria dc) {
		if ("SUB".equals(super.authorityCondition))
			addSubDC(dc, "emp", 1);
		addOrders(dc, page,
				new String[] {
						(new StringBuilder()).append(Empquit.PROP_EQ_DATE)
								.append("-down").toString(),
						(new StringBuilder()).append("emp.").append(
								Employee.PROP_EMP_DISTINCT_NO).append("-up")
								.toString() });
		BaseCrit.addEmpDC(dc, "emp", employeeId);
		BaseCrit.addDeptDC(dc, (new StringBuilder()).append("emp.").append(
				Employee.PROP_EMP_DEPT_NO).toString(), (new StringBuilder())
				.append("emp.").append(Employee.PROP_EMP_PB_NO).toString(),
				null, new Department(departmentId));
		BaseCrit.addDC(dc, Empquit.PROP_EQ_TYPE, "eq", new String[] { eqType });
		BaseCrit.addDC(dc, Empquit.PROP_EQ_REASON, "eq",
				new String[] { eqReason });
		BaseCrit.addEmpDC(dc, "manager", eqPermission);
		BaseCrit.addDC(dc, Empquit.PROP_ER_COMMENTS, "like",
				new String[] { eqComments });
		BaseCrit.addDCDateRange(dc, Empquit.PROP_EQ_DATE, eqDate, eqDateTo);
	}

	private void getDrillDownList() {
		eqTypeMap = ComonBeans.getValuesToMap(Empquit.PROP_EQ_TYPE,
				new boolean[] { true });
		eqReasonMap = ComonBeans.getSecondLevelSelect(eqTypeMap,
				Empquit.PROP_EQ_REASON, new boolean[] { true });
	}

	public String addQuit() throws Exception {
		if (StringUtils.isEmpty(employeeId)) {
			addErrorInfo("\u8BF7\u6C42\u7684\u53C2\u6570\u9519\u8BEF\uFF0C\u8BF7\u9009\u62E9\u8981\u79BB\u804C/\u590D\u804C\u7684\u5458\u5DE5\uFF01");
			return "success";
		}
		if (eqType.equals("0")) {
			if (eqDate == null) {
				addErrorInfo("\u79BB\u804C\u65E5\u671F\u4E0D\u80FD\u4E3A\u7A7A\uFF01");
				return "error";
			}
			if (StringUtils.isEmpty(eqPermission)) {
				addErrorInfo("\u79BB\u804C\u5BA1\u6279\u4EBA\u4E0D\u80FD\u4E3A\u7A7A\uFF01");
				return "error";
			}
		} else {
			if (eqDate == null) {
				addErrorInfo("\u590D\u804C\u65E5\u671F\u4E0D\u80FD\u4E3A\u7A7A\uFF01");
				return "error";
			}
			if (StringUtils.isEmpty(eqPermission)) {
				addErrorInfo("\u590D\u804C\u5BA1\u6279\u4EBA\u4E0D\u80FD\u4E3A\u7A7A\uFF01");
				return "error";
			}
			if (StringUtils.isEmpty(addPositionId)) {
				addErrorInfo("\u590D\u804C\u804C\u4F4D\u4E0D\u80FD\u4E3A\u7A7A\uFF01");
				return "error";
			}
			IClientBO clientLimit = (IClientBO) SpringBeanFactory
					.getBean("clientBo");
			String limitInfo = clientLimit.checkEmpLimit(1);
			if (!"SUCC".equals(limitInfo))
				return limitInfo;
			IPositionBo posBo = (IPositionBo) SpringBeanFactory
					.getBean("positionBo");
			String info = posBo.judgePostion(addPositionId, Integer.valueOf(1));
			if (!"SUCC".equals(info)) {
				addActionError(info);
				return "error";
			}
		}
		String currentEmpNo = getCurrentEmpNo();
		if (employeeId.equals(currentEmpNo)) {
			addErrorInfo("\u4E0D\u80FD\u5C06\u672C\u4EBA\u79BB\u804C\uFF01");
			return "success";
		}
		if ("SUB".equals(super.authorityCondition) && !checkAuth(employeeId)) {
			addErrorInfo("\u60A8\u6CA1\u6709\u4FEE\u6539\u6743\u9650\u6267\u884C\u672C\u64CD\u4F5C\uFF01");
			return "error";
		}
		UserBo userBo = (UserBo) getBean("userService");
		Userinfo oldUserinfo = userBo.getUserById(employeeId);
		if (userBo.checkAuthModule(oldUserinfo, "911")) {
			addErrorInfo("\u5458\u5DE5\u4E3A\u7CFB\u7EDF\u7BA1\u7406\u5458\uFF0C\u8BF7\u5148\u5230\u7CFB\u7EDF\u6A21\u5757\u53D6\u6D88\u8BE5\u7528\u6237\u7684\u7BA1\u7406\u5458\u6743\u9650\uFF0C\u65B9\u53EF\u79BB\u804C\uFF01");
			return "success";
		}
		if ("demo".equals((String) getSession().getAttribute("loginModel"))
				&& oldUserinfo.getUiUsername().length() >= 5
				&& "TYHRM".equalsIgnoreCase(oldUserinfo.getUiUsername()
						.substring(0, 5))) {
			addErrorInfo("demo\u7248\u4E0D\u80FD\u79BB\u804C\u5458\u5DE5\u7F16\u53F7\u4EE5TYHRM\u5F00\u5934\u7684\u5458\u5DE5\uFF01");
			return "success";
		}
		Empquit empquit = new Empquit();
		empquit.setEmployee(new Employee(employeeId));
		empquit.setEqDate(eqDate);
		empquit.setEqPermission(new Employee(eqPermission));
		empquit.setEqReason(eqReason);
		empquit.setEqType(eqType.toString());
		empquit.setErComments(eqComments);
		empquit.setEqCreateBy(currentEmpNo);
		empquit.setEqCreateDate(new Date());
		empquit.setEqLastChangeBy(currentEmpNo);
		empquit.setEqLastChangeTime(new Date());
		empquit.setPosition(new Position(addPositionId));
		IEmpQuitBo empQuitBo = (IEmpQuitBo) getBean("empQuitBo");
		String result = empQuitBo.saveEmpQuit(empquit);
		if (!"success".equals(result)) {
			addActionError(result);
			return "error";
		} else {
			setQRMap();
			addActionMessage("\u65B0\u589E\u5458\u5DE5\u79BB\u804C\u4FE1\u606F\u6210\u529F\u3002");
			return "success";
		}
	}

	public String deleteQuit() throws Exception {
		IEmpQuitBo empConstractBo = (IEmpQuitBo) getBean("empQuitBo");
		Empquit etf = empConstractBo.getById(updateEqId);
		if (etf == null)
			return "error";
		if ("SUB".equals(authorityCondition)
				&& !checkAuth(etf.getEmployee().getId())) {
			addErrorInfo("\u60A8\u6CA1\u6709\u5220\u9664\u6743\u9650\u6267\u884C\u672C\u64CD\u4F5C\uFF01");
			return "error";
		}
		String error = empConstractBo.delete(updateEqId, getCurrentEmp());
		if (StringUtils.isEmpty(error))
			addActionMessage((new StringBuilder()).append("\u5220\u9664")
					.append(etf.getEmployee().getEmpName()).append(
							"1".equals(etf.getEqType()) ? "\u590D\u804C"
									: "\u79BB\u804C").append(
							"\u4FE1\u606F\u6210\u529F\u3002").toString());
		else
			addErrorInfo(error);
		return "success";
	}

	public String updateQuit() throws Exception {
		String currentEmpNo = getCurrentEmpNo();
		if (employeeId == null || "".equals(employeeId))
			employeeId = currentEmpNo;
		if (updateEqId == null || "".equals(updateEqId))
			return "noId";
		if ("SUB".equals(super.authorityCondition) && !checkAuth(employeeId)) {
			addErrorInfo("\u60A8\u6CA1\u6709\u4FEE\u6539\u6743\u9650\u6267\u884C\u672C\u64CD\u4F5C\uFF01");
			return "success";
		} else {
			Empquit empquit = new Empquit();
			empquit.setEmployee(new Employee(employeeId));
			empquit.setEqDate(eqDate);
			empquit.setEqPermission(new Employee(eqPermission));
			empquit.setEqReason(equReason);
			empquit.setEqType(eqType.toString());
			empquit.setErComments(eqComments);
			empquit.setEqLastChangeBy(currentEmpNo);
			empquit.setEqLastChangeTime(new Date());
			IEmpQuitBo empQuitBo = (IEmpQuitBo) getBean("empQuitBo");
			empQuitBo.update(empquit, updateEqId, getCurrentEmp());
			clear();
			addActionMessage("\u66F4\u65B0\u5458\u5DE5\u79BB\u804C\u4FE1\u606F\u6210\u529F\u3002");
			setQRMap();
			return "success";
		}
	}

	private void setQRMap() {
		eqTypeMap = ComonBeans.getValuesToMap(Empquit.PROP_EQ_TYPE,
				new boolean[] { false });
		eqReasonMap = ComonBeans.getSecondLevelSelect(eqTypeMap,
				Empquit.PROP_EQ_REASON, new boolean[0]);
		eqRTypeMap = new TreeMap();
		eqRReasonMap = new TreeMap();
		eqRTypeMap.put("1", eqTypeMap.get("1"));
		eqRReasonMap.put("1", eqReasonMap.get("1"));
		eqTypeMap.remove("1");
		eqReasonMap.remove("1");
	}

	public String getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(String employeeId) {
		this.employeeId = employeeId;
	}

	public String getEqType() {
		return eqType;
	}

	public void setEqType(String eqType) {
		this.eqType = eqType;
	}

	public Date getEqDate() {
		return eqDate;
	}

	public void setEqDate(Date eqDate) {
		this.eqDate = eqDate;
	}

	public String getEqReason() {
		return eqReason;
	}

	public void setEqReason(String eqReason) {
		this.eqReason = eqReason;
	}

	public String getEqComments() {
		return eqComments;
	}

	public void setEqComments(String eqComments) {
		this.eqComments = eqComments;
	}

	public String getEqPermission() {
		return eqPermission;
	}

	public void setEqPermission(String eqPermission) {
		this.eqPermission = eqPermission;
	}

	public String getUpdateEqId() {
		return updateEqId;
	}

	public void setUpdateEqId(String updateEqId) {
		this.updateEqId = updateEqId;
	}

	public List getQuitList() {
		return quitList;
	}

	public void setQuitList(List quitList) {
		this.quitList = quitList;
	}

	public Date getEqDateTo() {
		return eqDateTo;
	}

	public void setEqDateTo(Date eqDateTo) {
		this.eqDateTo = eqDateTo;
	}

	private void clear() {
		employeeId = null;
		eqDate = null;
		eqPermission = null;
		eqReason = null;
		eqType = null;
		eqComments = null;
		updateEqId = null;
	}

	public String getEquReason() {
		return equReason;
	}

	public void setEquReason(String equReason) {
		this.equReason = equReason;
	}

	public Pager getPage() {
		return page;
	}

	public void setPage(Pager page) {
		this.page = page;
	}

	public String getOutmatchModelId() {
		return outmatchModelId;
	}

	public void setOutmatchModelId(String outmatchModelId) {
		this.outmatchModelId = outmatchModelId;
	}

	public String getOutputIoName() {
		return outputIoName;
	}

	public void setOutputIoName(String outputIoName) {
		this.outputIoName = outputIoName;
	}

	public String getSearchOrExport() {
		return searchOrExport;
	}

	public void setSearchOrExport(String searchOrExport) {
		this.searchOrExport = searchOrExport;
	}

	public Map getEqTypeMap() {
		return eqTypeMap;
	}

	public Map getEqReasonMap() {
		return eqReasonMap;
	}

	public String getDepartmentId() {
		return departmentId;
	}

	public void setDepartmentId(String departmentId) {
		this.departmentId = departmentId;
	}

	public Employee getQuitemp() {
		return quitemp;
	}

	public void setQuitemp(Employee quitemp) {
		this.quitemp = quitemp;
	}

	public String getEmpNo() {
		return empNo;
	}

	public void setEmpNo(String empNo) {
		this.empNo = empNo;
	}

	public String getAddPositionId() {
		return addPositionId;
	}

	public void setAddPositionId(String addPositionId) {
		this.addPositionId = addPositionId;
	}

	public String getDepartmentName() {
		return departmentName;
	}

	public void setDepartmentName(String departmentName) {
		this.departmentName = departmentName;
	}

	private static final long serialVersionUID = 1L;
	private static final String AUTHMODULE = "911";
	private String employeeId;
	private String eqPermission;
	private String departmentId;
	private String departmentName;
	private Date eqDate;
	private Date eqDateTo;
	private String eqType;
	private String eqReason;
	private String eqComments;
	private String addPositionId;
	private Map eqTypeMap;
	private Map eqReasonMap;
	private Map eqRTypeMap;
	private Map eqRReasonMap;
	private Employee quitemp;
	private String empNo;
	private String equReason;
	private String updateEqId;
	private List quitList;
	private Pager page;
	private String outmatchModelId;
	private String outputIoName;
	private String searchOrExport;
}
